﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Security.Cryptography;
using System.Text;
using FlyingClub.Models;
using FlyingClub.Models.ViewsModels;
using System.Net.Mail;
using System.Configuration;
using System.Web.Helpers;

namespace FlyingClub.Controllers
{
	public class BeforeController : Controller
	{
		//
		// GET: /Before/
 
		public int GetAccess()
		{
			if (Session.Count != 0)
			{
				if (Session["Staff"] != null)
					return 2;
				else
					return 1;
			}
			else
				return 0;
		}

		public string GetMd5Hash(string input)
		{
			// Create a new instance of the MD5CryptoServiceProvider object.
			MD5 md5Hasher = MD5.Create();

			// Convert the input string to a byte array and compute the hash.
			byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

			// Create a new Stringbuilder to collect the bytes and create a string.
			StringBuilder sBuilder = new StringBuilder();

			// Loop through each byte of the hashed data and format each one as a hexadecimal string.
			for (int i = 0; i < data.Length; i++)
			{
				sBuilder.Append(data[i].ToString("x2"));
			}

			// Return the hexadecimal string.
			return sBuilder.ToString();
		}

		// Verify a hash against a string.
		public bool VerifyMd5Hash(string input, string hash)
		{
			// Hash the input.
			string hashOfInput = this.GetMd5Hash(input);

			// Create a StringComparer an compare the hashes.
			StringComparer comparer = StringComparer.OrdinalIgnoreCase;

			if (0 == comparer.Compare(hashOfInput, hash))
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public string MD5Encrypt(string input)
		{
			string passwordMD5 = this.GetMd5Hash(input);
			while (!this.VerifyMd5Hash(input, passwordMD5))
				passwordMD5 = this.GetMd5Hash(input);

			return passwordMD5;
		}
		
		public bool MemberIsAvailable(int id, DateTime dateTime)
		{
			using (FlyingClubDBEntities context = new FlyingClubDBEntities())
			{
				List<Flight> flights = context.Flight.OrderByDescending(x => x.ID).Where(x => x.PilotID == id).ToList();

				foreach (Flight flight in flights)
				{
					if (flight.DepartureDate <= dateTime && flight.ArrivalDate >= dateTime)
						return false;
				}

				return true;
			}
		}

		public bool PlaneIsAvailable(int planeID, DateTime dateTime)
		{
			using (FlyingClubDBEntities context = new FlyingClubDBEntities())
			{
				List<Flight> flights = context.Flight.OrderByDescending(x => x.ID).Where(x => x.PlaneID == planeID).ToList();

				foreach (Flight flight in flights)
				{
					if (flight.DepartureDate <= dateTime && flight.ArrivalDate >= dateTime)
						return false;
				}
			}
			return true;
		}

		public MemberCreateViewModel MemberToCreateViewModel(Member member)
		{
			MemberCreateViewModel m = new MemberCreateViewModel();
			m.ID = member.ID;
			m.Email = member.Email;
			m.Password = member.Password;
			m.Firstname = member.Firstname;
			m.Lastname = member.Lastname;
			m.InstructorNumber = (int)member.InstructorNumber;
			m.Wallet = (int)member.Wallet;
			m.Staff = member.Staff;
			m.AddressNumber = member.AddressNumber;
			m.AddressStreet = member.AddressStreet;
			m.AddressCode = member.AddressCode;
			m.AddressCity = member.AddressCity;
			m.AddressCountry = member.AddressCountry;
			m.Gender = member.Gender;
			m.AdhesionDate = member.AdhesionDate;
			m.AdhesionFees = member.AdhesionFees;

			return m;
		}

		public Member MemberCreateViewModelToMember(MemberCreateViewModel member)
		{
			Member m = new Member();
			m.ID = member.ID;
			m.Email = member.Email;
			m.Password = member.Password;
			m.Firstname = member.Firstname;
			m.Lastname = member.Lastname;
			m.InstructorNumber = (int)member.InstructorNumber;
			m.Wallet = (int)member.Wallet;
			m.AdhesionDate = member.AdhesionDate;
			m.AdhesionFees = member.AdhesionFees;
			m.Staff = member.Staff;
			m.AddressNumber = member.AddressNumber;
			m.AddressStreet = member.AddressStreet;
			m.AddressCode = member.AddressCode;
			m.AddressCity = member.AddressCity;
			m.AddressCountry = member.AddressCountry;
			m.Gender = member.Gender;

			return m;
		}

		public void SendEmail(string memberEmail, string subject, string body)
		{
			WebMail.SmtpServer = "smtp.gmail.com";
			WebMail.EnableSsl = true;
			WebMail.SmtpPort = 587;
			WebMail.UserName = "flyingclubproject@gmail.com";
			WebMail.Password = "supinfo2012";

			try 
			{
				WebMail.Send(memberEmail, subject, body, "flyingclubproject@gmail.com");
			}
			catch (Exception)
			{

			}
		}

		public bool ReceiveEmail(string name, string email, string subject, string body)
		{
			WebMail.SmtpServer = "smtp.gmail.com";
			WebMail.EnableSsl = true;
			WebMail.SmtpPort = 587;
			WebMail.UserName = "flyingclubproject@gmail.com";
			WebMail.Password = "supinfo2012";

			try
			{
				WebMail.Send("flyingclubproject@gmail.com", name + " vous contacte au sujet de : " + subject, body + "\n\nSon email : " + email, "flyingclubproject@gmail.com");
			}
			catch (Exception)
			{
				return false;
			}

			return true;
		}
	}
}
